package com.kfm.am.exec;

import java.io.*;

public class Exec01 {

    public static void main(String[] args) {
        // 使用字节输出流将一组整数写入到文件中，并使用字节输入流读取这些整数并计算它们的总和。
        int[] nums = {11, 22, 33, 44};
        String name = "E:\\Temp\\io\\test.txt";
        File file = new File(name);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try(FileOutputStream out = new FileOutputStream(file);
            FileInputStream in = new FileInputStream(file)
        ) {
            String str = "";
            for (int num : nums) {
                str += String.valueOf(num) + ",";
            }
            str = str.substring(0, str.length() - 1);
            byte[] bytes = str.getBytes();
            out.write(bytes);
            byte[] allBytes = in.readAllBytes();
            String num = new String(allBytes);
            String[] split = num.split(",");
            int sum = 0;
            for (String s : split) {
                System.out.println(s);
                sum += Integer.parseInt(s);
            }
            System.out.println(sum);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
